<?php
/**
 * Created by PhpStorm.
 * User: dllo
 * Date: 16/8/22
 * Time: 上午11:45
 */

//提交姓名和电话
//api:shuqian_api.php?type=submit&name=xx&tel=xx&openid=xx
//返回值:{err:1,msg:"失败原因"}

//保存分数
//api:shuqian_api.php?type=score&openid=xx&score=100
//返回值:{err:1,msg:"网络错误",rank:10}
$type = $_GET["type"];

mysql_connect(SAE_MYSQL_HOST_M.":".SAE_MYSQL_PORT,SAE_MYSQL_USER,SAE_MYSQL_PASS);
mysql_select_db(SAE_MYSQL_DB);
mysql_query("set names utf8");


$jiangpin = rand(0,11);

switch ($type){
    case "zhuanpan":
        $openid = $_GET["openid"];

        $sql = "UPDATE chong SET jiangpin={$jiangpin} WHERE openid='{$openid}'";
        mysql_query($sql);
        if(mysql_affected_rows()>0){
            $arr = array("err"=>0,"msg"=>"成功","jiangpin"=>$jiangpin);
            echo json_encode($arr);
        }else{
            echo '{"err":1,"msg":"更新失败,请检查输入的内容"}';
        }
        break;
    case "submit":
        $openid = $_GET["openid"];
        $name = $_GET["name"];
        $tel = $_GET["tel"];
        $address = $_GET["address"];
        $sql = "UPDATE chong SET name='{$name}',tel='{$tel}',address='{$address}' WHERE openid='{$openid}'";
        mysql_query($sql);
        if(mysql_affected_rows()){
            echo '{"err":0,"msg":"成功"}';
        }else{
            echo '{"err":1,"msg":"更新失败,请检查输入的内容"}';
        }
        break;
    case "fen":
        $openid = $_GET["openid"];
        $score = $_GET["score"];
        $sql = "UPDATE chong SET score='{$score}' WHERE openid='{$openid}'";
        mysql_query($sql);
        if (mysql_affected_rows()){
            echo '{"err":0,"msg":"成功"}';
        }else{
            $arr = array("err"=>1,"msg"=>"失败","score"=>$score);
            echo json_encode($arr);
        }
        break;

}

